package com.cdc.rxjava;

import org.reactivestreams.Subscriber;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by cdc on 2018/3/30.
 */

public class RxjavaCreateDemo {


    //第一步：创建被观察者：create
    Observable observable = Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            emitter.onNext("Hello");
            emitter.onNext("Imooc");
            emitter.onComplete();
        }
    });

    //通过just方法来创建被观察者
    Observable observableJust = Observable.just("hello", "Imooc");

    //通过from方法来创建被观察者
    String[] parameters = {"hello", "Imooc"};
    Observable observableFrom = Observable.fromArray(parameters);

    //第二步：创建观察者
    Observer<Object> observer = new Observer<Object>() {


        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Object o) {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    };

    public void doRxjava(){
        //第三步：订阅
        observable.subscribe(observer);
    }

}

